package com.linln.wxapi;

import com.linln.common.constant.AdminConst;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.vo.ResultVo;
import com.linln.modules.member.domain.AreaCount;
import com.linln.modules.member.domain.MemberAgent;
import com.linln.modules.member.domain.MemberInfo;
import com.linln.modules.member.domain.MemberOrder;
import com.linln.modules.member.service.MemberOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author hct
 * @date 2019/09/12
 */
@Api(value="订单信息管理")
@RestController
@RequestMapping("/wx/order/{area}")
public class WxApiMemberOrderController {

    @Autowired
    private MemberOrderService memberOrderService;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     * 我的订单列表

     * @return
     */
    @ApiOperation(value = "获取订单列表信息",  notes = "获取订单列表信息")
    @RequestMapping(value = "list",method={RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public ResultVo list(@PathVariable String area,Long memberId,
    		@RequestParam(value = "pageNumber", required = false,defaultValue="1") Integer pageNumber,
			@RequestParam(value = "pageSize", required = false,defaultValue="20") Integer pageSize
    		) {

        try {
            ExampleMatcher matcher = ExampleMatcher.matching();
            MemberOrder memberOrder=new MemberOrder();
            MemberInfo member=new MemberInfo();
            member.setId(memberId);
            memberOrder.setMember(member);
            // 获取数据列表
            Example<MemberOrder> example = Example.of(memberOrder, matcher);
            Page<MemberOrder> list = memberOrderService.getPageList(example,pageNumber,pageSize);
            return ResultVoUtil.success(list);
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            return ResultVoUtil.error("接口异常");
        }
    }


    /**
     * 列表页面
     */
    @GetMapping("/count")
    @ApiOperation(value = "获取订单数量",  notes = "获取订单数量")
    @RequestMapping(value = "count",method={RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public ResultVo count(@PathVariable String area,Long memberId) {
        ExampleMatcher matcher = ExampleMatcher.matching();
        MemberOrder memberOrder=new MemberOrder();
        MemberInfo member=new MemberInfo();
        member.setId(memberId);
        memberOrder.setMember(member);

        // 获取数据列表
        Example<MemberOrder> example = Example.of(memberOrder, matcher);
        Long count = memberOrderService.countOrder(example);
        Map map=new HashMap();
        map.put("count",count);
        return ResultVoUtil.success(map);


    }


}